import { defineStore } from 'pinia'
import { cartList,cartCheck,changeCartNum } from '@/api/cart';
import { IarrayCartData } from '@/types/goods'

// 购物车
export const useCartStore = defineStore('cart', {
    state: () => {
      return {
        cartData:[] as IarrayCartData,
        isAllCheck:false as boolean
      }
    },
    getters:{
      // 合计 ： 元 + 00
      countPrice: (state) => {
        let all = 0
        for (const iterator of state.cartData) {
            if(iterator.is_checked === 1){
                all += iterator.goods.price * iterator.num
            }
        }
        return Number(all + '00') // 总价
      }
    },
    actions: {
        // 转换是否选中属性值 1 = true , 0 = false
        handleChangeType(){
            for (const key in this.cartData) {
            if(this.cartData[key].is_checked === true){
                this.cartData[key].is_checked = 1
            }else if(this.cartData[key].is_checked === false){
                this.cartData[key].is_checked = 0
            }
            }
        },
        // 判断是否全选
        handleIsAllCheck(){
            this.handleChangeType()
            let count = 0
            for (const iterator of this.cartData) {
            if(iterator.is_checked === 1){
                count++
            }
            }
            if(this.cartData.length === count){
            this.isAllCheck = true
            }else{
            this.isAllCheck = false
            }
        },
        // 获取购物车页面数据
        async getCartList () {
            let res = await cartList()
            if(res.status === 200){
            this.cartData = res.data.data
            this.handleIsAllCheck()
            }
        },
        // 发送请求改变选中的项
        async sendCartCheck () {
            let newArr = []
            for (let iterator of this.cartData) {
            if(iterator.is_checked === 1){
                newArr.push(iterator.id)
            }
            }
            let res = await cartCheck(newArr)
            if(res.status === 204){
            // 成功
            }
        },
        // 单个选中按钮
        handleCheck(id:number){
            this.handleChangeType()
            for (const key in this.cartData) {
            if(this.cartData[key].id === id){
                this.cartData[key].is_checked === 0 ? 1 : 0
            }
            }
            this.handleIsAllCheck()
            this.sendCartCheck()
        },
        // 全部选中按钮
        handleAllCheck(){
            this.handleChangeType()
            if(this.isAllCheck === true){
            for (const key in this.cartData) {
                this.cartData[key].is_checked = 1
            }
            }else if(this.isAllCheck === false){
            for (const key in this.cartData) {
                this.cartData[key].is_checked = 0
            }
            }
            this.sendCartCheck()
        },
        // 改变购物车单个商品数量
        async handleChangeNum(id:number,num:number){
          let res = await changeCartNum(id,num)
          if(res.status === 204){
            // 成功
          }
        }
    }
  })